package com.snj.visangedu.news;

import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.snj.visangedu.R;
import com.snj.visangedu.lib.CustomHttpClient;
import com.snj.visangedu.lib.Global;


public class New_NewsAdapter extends BaseAdapter {
	
	String tag = "New_NewsAdapter";
	
	Context context;
	int layout;
	
	int newsid = 0;
	
	
	ArrayList<News> mNewsList;
	
	LayoutInflater mInflater = null;
	
	public New_NewsAdapter(Context _context, int _layout, ArrayList<News> _newslist, int _newsid) {
		// TODO Auto-generated constructor stub
		
		this.context = _context;
		this.layout = _layout;
		this.mNewsList = _newslist;
		this.newsid = _newsid;
		
	}
	
	public void Insert(ArrayList<News> mList)
	{
		mNewsList.addAll(mList);
	}
	
	public void SetList(ArrayList<News> mList)
	{
		mNewsList = mList;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return mNewsList.size();
	}

	public News getItem(int position) {
		// TODO Auto-generated method stub
		return mNewsList.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
	
		final NewsHolder holder;
		
		if(convertView == null)
		{
			mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = mInflater.inflate(layout, parent, false);
			
			holder = new NewsHolder();
			
			holder.img_news = (ImageView)convertView.findViewById(R.id.img_news);
			holder.txt_title = (TextView)convertView.findViewById(R.id.txt_newstitle);
			holder.txt_content = (TextView)convertView.findViewById(R.id.txt_newssubtitle);
			
			convertView.setTag(holder);
			
			
		}
		else
		{
			holder = (NewsHolder)convertView.getTag();
		}
		
		/*
		if(!mNewsList.get(position).GetDrawn())
		{
		
			Animation ani = AnimationUtils.loadAnimation(context, R.anim.listani);
			ani.setStartOffset(position * 50);
			convertView.startAnimation(ani);
			mNewsList.get(position).SetDrawn(true);
		}
		
		Log.d(tag, "Created news item");
		*/
		
		holder.img_news.setBackgroundResource(android.R.color.transparent);
		
		Bitmap b = BitmapFactory.decodeFile(mNewsList.get(position).GetFilename());
		Log.d(tag, "Load : " + mNewsList.get(position).GetFilename());
		
		if(b == null)
		{
			Log.d(tag, "Load failed");
			new AsyncTask<String, String, Bitmap>() {

				@Override
				protected Bitmap doInBackground(String... params) {
					// TODO Auto-generated method stub
					
					Bitmap newsimg = null;
					
					try
					{
					
						String currentpath = "visangedu/news" + String.format("%d", (newsid + 1));// + "/";
						String filename = params[0];//mNewsList.get(position).GetOriginalFileName().replace(".jpg", ".vnews").replace(".png", ".vnews");
						
						
						newsimg = CustomHttpClient.downloadBitmap(Global.vnpbaseurl + "newsimages" + String.format("%d", (newsid + 1)) + "/" + mNewsList.get(position).GetOriginalFileName());
												
						filename = filename.replace(currentpath, "");
						filename = filename.replace(Global.ex_storage, "");
						Global.saveBitmaptoJpeg(newsimg, currentpath, filename);
						Log.d(tag, "Save : " + currentpath + filename);
						Log.d(tag, "Download : " + filename);
					}
					catch(Exception e)
					{
						//Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
					}
					
					return newsimg;
				}
 
				@Override
				protected void onPostExecute(Bitmap result) {
					// TODO Auto-generated method stub
					super.onPostExecute(result);
					
					mNewsList.get(position).SetNewsImage(result);
					holder.img_news.setImageBitmap(result);
				}
				
			}.execute(mNewsList.get(position).GetFilename());
		}
		else
		{
			mNewsList.get(position).SetNewsImage(b);
			holder.img_news.setImageBitmap(b);
		}
		//holder.txt_title.setText(mNewsList.get(position).GetTitle());
		//holder.txt_content.setText(mNewsList.get(position).GetContent());
		
		
		
		
		return convertView;
	}
	
	
}
